package javolution.lang;

import java.lang.reflect.InvocationTargetException;
import javolution.JavolutionError;
import javolution.util.FastMap;

/* loaded from: classes2.dex */
public final class Reflection {
    private static final FastMap _NameToClass = new FastMap();
    private static final Object[] ARRAY_0 = new Object[0];
    private static final ThreadLocal ARRAY_1 = new ThreadLocal() { // from class: javolution.lang.Reflection.1
        @Override // java.lang.ThreadLocal
        protected Object initialValue() {
            return new Object[1];
        }
    };
    private static final ThreadLocal ARRAY_2 = new ThreadLocal() { // from class: javolution.lang.Reflection.2
        @Override // java.lang.ThreadLocal
        protected Object initialValue() {
            return new Object[2];
        }
    };
    private static final ThreadLocal ARRAY_3 = new ThreadLocal() { // from class: javolution.lang.Reflection.3
        @Override // java.lang.ThreadLocal
        protected Object initialValue() {
            return new Object[3];
        }
    };
    private static final ThreadLocal ARRAY_4 = new ThreadLocal() { // from class: javolution.lang.Reflection.4
        @Override // java.lang.ThreadLocal
        protected Object initialValue() {
            return new Object[4];
        }
    };

    /* loaded from: classes2.dex */
    public static abstract class Constructor {
        private final Class[] _parameterTypes;

        protected Constructor(Class[] clsArr) {
            this._parameterTypes = clsArr;
        }

        protected abstract Object allocate(Object[] objArr);

        public Class[] getParameterTypes() {
            return this._parameterTypes;
        }

        public final Object newInstance() {
            if (this._parameterTypes.length == 0) {
                return allocate(Reflection.ARRAY_0);
            }
            throw new IllegalArgumentException("Expected number of parameters is " + this._parameterTypes.length);
        }

        public final Object newInstance(Object obj) {
            if (this._parameterTypes.length != 1) {
                throw new IllegalArgumentException("Expected number of parameters is " + this._parameterTypes.length);
            }
            Object[] objArr = (Object[]) Reflection.ARRAY_1.get();
            objArr[0] = obj;
            Object allocate = allocate(objArr);
            objArr[0] = null;
            return allocate;
        }

        public final Object newInstance(Object obj, Object obj2) {
            if (this._parameterTypes.length != 2) {
                throw new IllegalArgumentException("Expected number of parameters is " + this._parameterTypes.length);
            }
            Object[] objArr = (Object[]) Reflection.ARRAY_2.get();
            objArr[0] = obj;
            objArr[1] = obj2;
            Object allocate = allocate(objArr);
            objArr[0] = null;
            objArr[1] = null;
            return allocate;
        }

        public final Object newInstance(Object obj, Object obj2, Object obj3) {
            if (this._parameterTypes.length != 3) {
                throw new IllegalArgumentException("Expected number of parameters is " + this._parameterTypes.length);
            }
            Object[] objArr = (Object[]) Reflection.ARRAY_3.get();
            objArr[0] = obj;
            objArr[1] = obj2;
            objArr[2] = obj3;
            Object allocate = allocate(objArr);
            objArr[0] = null;
            objArr[1] = null;
            objArr[2] = null;
            return allocate;
        }

        public final Object newInstance(Object obj, Object obj2, Object obj3, Object obj4) {
            if (this._parameterTypes.length != 4) {
                throw new IllegalArgumentException("Expected number of parameters is " + this._parameterTypes.length);
            }
            Object[] objArr = (Object[]) Reflection.ARRAY_4.get();
            objArr[0] = obj;
            objArr[1] = obj2;
            objArr[2] = obj3;
            objArr[3] = obj4;
            Object allocate = allocate(objArr);
            objArr[0] = null;
            objArr[1] = null;
            objArr[2] = null;
            objArr[3] = null;
            return allocate;
        }
    }

    /* loaded from: classes2.dex */
    private static class DefaultConstructor extends Constructor {
        final Class _class;

        DefaultConstructor(Class cls) {
            super(new Class[0]);
            this._class = cls;
        }

        @Override // javolution.lang.Reflection.Constructor
        public Object allocate(Object[] objArr) {
            try {
                return this._class.newInstance();
            } catch (IllegalAccessException e) {
                throw new JavolutionError("Illegal access error for " + this._class.getName() + " constructor", e);
            } catch (InstantiationException e2) {
                throw new JavolutionError("Instantiation error for " + this._class.getName() + " default constructor", e2);
            }
        }

        public String toString() {
            return this._class + " default constructor";
        }
    }

    /* loaded from: classes2.dex */
    public static abstract class Method {
        private final Class[] _parameterTypes;

        protected Method(Class[] clsArr) {
            this._parameterTypes = clsArr;
        }

        protected abstract Object execute(Object obj, Object[] objArr);

        public Class[] getParameterTypes() {
            return this._parameterTypes;
        }

        public final Object invoke(Object obj) {
            return execute(obj, Reflection.ARRAY_0);
        }

        public final Object invoke(Object obj, Object obj2) {
            if (this._parameterTypes.length != 1) {
                throw new IllegalArgumentException("Expected number of parameters is " + this._parameterTypes.length);
            }
            Object[] objArr = (Object[]) Reflection.ARRAY_1.get();
            objArr[0] = obj2;
            Object execute = execute(obj, objArr);
            objArr[0] = null;
            return execute;
        }

        public final Object invoke(Object obj, Object obj2, Object obj3) {
            if (this._parameterTypes.length != 2) {
                throw new IllegalArgumentException("Expected number of parameters is " + this._parameterTypes.length);
            }
            Object[] objArr = (Object[]) Reflection.ARRAY_2.get();
            objArr[0] = obj2;
            objArr[1] = obj3;
            Object execute = execute(obj, objArr);
            objArr[0] = null;
            objArr[1] = null;
            return execute;
        }

        public final Object invoke(Object obj, Object obj2, Object obj3, Object obj4) {
            if (this._parameterTypes.length != 3) {
                throw new IllegalArgumentException("Expected number of parameters is " + this._parameterTypes.length);
            }
            Object[] objArr = (Object[]) Reflection.ARRAY_3.get();
            objArr[0] = obj2;
            objArr[1] = obj3;
            objArr[2] = obj4;
            Object execute = execute(obj, objArr);
            objArr[0] = null;
            objArr[1] = null;
            objArr[2] = null;
            return execute;
        }

        public final Object invoke(Object obj, Object obj2, Object obj3, Object obj4, Object obj5) {
            if (this._parameterTypes.length != 3) {
                throw new IllegalArgumentException("Expected number of parameters is " + this._parameterTypes.length);
            }
            Object[] objArr = (Object[]) Reflection.ARRAY_3.get();
            objArr[0] = obj2;
            objArr[1] = obj3;
            objArr[2] = obj4;
            objArr[3] = obj5;
            Object execute = execute(obj, objArr);
            objArr[0] = null;
            objArr[1] = null;
            objArr[2] = null;
            objArr[3] = null;
            return execute;
        }
    }

    /* loaded from: classes2.dex */
    private static final class ReflectConstructor extends Constructor {
        private final String _signature;
        private final java.lang.reflect.Constructor _value;

        public ReflectConstructor(java.lang.reflect.Constructor constructor, String str) {
            super(constructor.getParameterTypes());
            this._value = constructor;
            this._signature = str;
        }

        @Override // javolution.lang.Reflection.Constructor
        public Object allocate(Object[] objArr) {
            try {
                return this._value.newInstance(objArr);
            } catch (IllegalAccessException e) {
                throw new JavolutionError("Illegal access error for " + this._signature + " constructor", e);
            } catch (IllegalArgumentException e2) {
                throw new JavolutionError("Illegal argument for " + this._signature + " constructor", e2);
            } catch (InstantiationException e3) {
                throw new JavolutionError("Instantiation error for " + this._signature + " constructor", e3);
            } catch (InvocationTargetException e4) {
                throw new JavolutionError("Invocation exception  for " + this._signature + " constructor", (InvocationTargetException) e4.getTargetException());
            }
        }

        public String toString() {
            return this._signature + " constructor";
        }
    }

    /* loaded from: classes2.dex */
    private static final class ReflectMethod extends Method {
        private final String _signature;
        private final java.lang.reflect.Method _value;

        public ReflectMethod(java.lang.reflect.Method method, String str) {
            super(method.getParameterTypes());
            this._value = method;
            this._signature = str;
        }

        @Override // javolution.lang.Reflection.Method
        public Object execute(Object obj, Object[] objArr) {
            try {
                return this._value.invoke(obj, objArr);
            } catch (IllegalAccessException e) {
                throw new JavolutionError("Illegal access error for " + this._signature + " method", e);
            } catch (IllegalArgumentException e2) {
                throw new JavolutionError("Illegal argument for " + this._signature + " method", e2);
            } catch (InvocationTargetException e3) {
                throw new JavolutionError("Invocation exception for " + this._signature + " method", (InvocationTargetException) e3.getTargetException());
            }
        }

        public String toString() {
            return this._signature + " method";
        }
    }

    private Reflection() {
    }

    private static Class classFor(String str) throws ClassNotFoundException {
        StringBuilder sb;
        String str2;
        int indexOf = str.indexOf("[]");
        if (indexOf < 0) {
            return str.equals("boolean") ? Boolean.TYPE : str.equals("byte") ? Byte.TYPE : str.equals("char") ? Character.TYPE : str.equals("short") ? Short.TYPE : str.equals("int") ? Integer.TYPE : str.equals("long") ? Long.TYPE : str.equals("float") ? Float.TYPE : str.equals("double") ? Double.TYPE : getClass(str);
        }
        if (str.indexOf("[][]") < 0) {
            sb = new StringBuilder();
            str2 = "[";
        } else if (str.indexOf("[][][]") < 0) {
            sb = new StringBuilder();
            str2 = "[[";
        } else if (str.indexOf("[][][][]") >= 0) {
            throw new UnsupportedOperationException("The maximum array dimension is 3");
        } else {
            sb = new StringBuilder();
            str2 = "[[[";
        }
        sb.append(str2);
        sb.append(descriptorFor(str.substring(0, indexOf)));
        return getClass(sb.toString());
    }

    private static Class[] classesFor(String str) throws ClassNotFoundException {
        String trim = str.trim();
        if (trim.length() == 0) {
            return new Class[0];
        }
        int i = 0;
        int i2 = 0;
        while (true) {
            int indexOf = trim.indexOf(44, i);
            int i3 = indexOf + 1;
            if (indexOf < 0) {
                break;
            }
            i2++;
            i = i3;
        }
        Class[] clsArr = new Class[i2 + 1];
        int i4 = 0;
        for (int i5 = 0; i5 < i2; i5++) {
            int indexOf2 = trim.indexOf(44, i4);
            clsArr[i5] = classFor(trim.substring(i4, indexOf2).trim());
            if (clsArr[i5] == null) {
                return null;
            }
            i4 = indexOf2 + 1;
        }
        clsArr[i2] = classFor(trim.substring(i4).trim());
        if (clsArr[i2] == null) {
            return null;
        }
        return clsArr;
    }

    private static String descriptorFor(String str) {
        if (str.equals("boolean")) {
            return "Z";
        }
        if (str.equals("byte")) {
            return "B";
        }
        if (str.equals("char")) {
            return "C";
        }
        if (str.equals("short")) {
            return "S";
        }
        if (str.equals("int")) {
            return "I";
        }
        if (str.equals("long")) {
            return "J";
        }
        if (str.equals("float")) {
            return "F";
        }
        if (str.equals("double")) {
            return "D";
        }
        return "L" + str + ";";
    }

    public static Class getClass(CharSequence charSequence) throws ClassNotFoundException {
        Class cls = (Class) _NameToClass.get(charSequence);
        return cls != null ? cls : searchClass(charSequence.toString());
    }

    public static Class getClass(String str) throws ClassNotFoundException {
        Class cls = (Class) _NameToClass.get(str);
        return cls != null ? cls : searchClass(str);
    }

    public static Constructor getConstructor(String str) {
        int indexOf = str.indexOf(40) + 1;
        if (indexOf >= 0) {
            int indexOf2 = str.indexOf(41);
            if (indexOf2 >= 0) {
                try {
                    Class cls = getClass(str.substring(0, indexOf - 1));
                    String substring = str.substring(indexOf, indexOf2);
                    return substring.length() == 0 ? new DefaultConstructor(cls) : new ReflectConstructor(cls.getConstructor(classesFor(substring)), str);
                } catch (ClassNotFoundException | NoSuchMethodException unused) {
                    return null;
                }
            }
            throw new IllegalArgumentException("Parenthesis ')' not found");
        }
        throw new IllegalArgumentException("Parenthesis '(' not found");
    }

    public static Method getMethod(String str) {
        int indexOf = str.indexOf(40) + 1;
        if (indexOf >= 0) {
            int indexOf2 = str.indexOf(41);
            if (indexOf2 >= 0) {
                int lastIndexOf = str.substring(0, indexOf).lastIndexOf(46) + 1;
                try {
                    return new ReflectMethod(getClass(str.substring(0, lastIndexOf - 1)).getMethod(str.substring(lastIndexOf, indexOf - 1), classesFor(str.substring(indexOf, indexOf2))), str);
                } catch (Throwable unused) {
                    return null;
                }
            }
            throw new IllegalArgumentException("Parenthesis ')' not found");
        }
        throw new IllegalArgumentException("Parenthesis '(' not found");
    }

    private static Class searchClass(String str) throws ClassNotFoundException {
        Class<?> cls;
        try {
            cls = Class.forName(str);
        } catch (ClassNotFoundException unused) {
            try {
                cls = Class.forName(str, true, Thread.currentThread().getContextClassLoader());
            } catch (ClassNotFoundException unused2) {
                cls = Class.forName(str, true, ClassLoader.getSystemClassLoader());
            }
            if (cls == null) {
                throw new ClassNotFoundException("Cannot found class " + str);
            }
        }
        synchronized (_NameToClass) {
            _NameToClass.put(str, cls);
        }
        return cls;
    }
}
